package com.misolab.utility.speedmate.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

import com.misolab.utility.speedmate.R;
import com.misolab.utility.speedmate.util.Control;
import com.misolab.utility.speedmate.util.Control.Lisenter;
import com.misolab.utility.speedmate.util.MyWindowManager;

public class MainActivity extends Activity {

	MyWindowManager mWindowManager;
	Control mControl1;
	Control mControl2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
		notificationManager.cancel(SPEEDMATE_NOTI_ID);

		for (int i = 0; i < 21; i++) {
			values[20 - i] = (float) (i * (0.1));
		}

		mWindowManager = new MyWindowManager();
		int index1 = 0;
		int index2 = 0;
		try {
			index1 = indexFromValue(mWindowManager.getAnimationScale(0));
			index2 = indexFromValue(mWindowManager.getAnimationScale(1));
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		mControl1 = new Control(this, R.id.control_1, index1);
		mControl1.setLisenter(new Lisenter() {

			@Override
			public void onClickPreview(Control control) {
				showPreview1();
			}

			@Override
			public void onClickInitialize(Control control) {
				control.setSelected(10, true);
			}

			@Override
			public void onSelected(Control control) {
				float value = valueFromIndex(control.getSelected());
				try {
					mWindowManager.setAnimationScale(0, value);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		mControl2 = new Control(this, R.id.control_2, index2);
		mControl2.setLisenter(new Lisenter() {

			@Override
			public void onClickPreview(Control control) {
				showPreiview2();
			}

			@Override
			public void onClickInitialize(Control control) {
				control.setSelected(10, true);
			}

			@Override
			public void onSelected(Control control) {
				float value = valueFromIndex(control.getSelected());
				try {
					mWindowManager.setAnimationScale(1, value);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshValues();
	}

	private void refreshValues() {
		int index1 = 0;
		int index2 = 0;
		try {
			index1 = indexFromValue(mWindowManager.getAnimationScale(0));
			index2 = indexFromValue(mWindowManager.getAnimationScale(1));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		mControl1.setSelected(index1, true);
		mControl2.setSelected(index2, true);
	}

	@Override
	protected void onDestroy() {
		log("onDestroy>>");
		super.onDestroy();
		if (getMenuSetting() == 0) {
			registerNotification();
		}
	}

	private void registerNotification() {
		log("registerNotification>>");
		CharSequence contentTitle = getString(R.string.app_name);
		CharSequence contentText = "";
		
		try {
			String message1 = getString(R.string.window_animations);
			String message2 = getString(R.string.transition_animations);
			String value1 = getStringValue( indexFromValue(mWindowManager.getAnimationScale(0)) );
			String value2 = getStringValue( indexFromValue(mWindowManager.getAnimationScale(1)) );
			contentText = String.format("*%s:%s *%s:%s", message1, value1, message2, value2);
		} catch (RemoteException e) {
			e.printStackTrace();
			contentText="";
		}
		
		// Get a reference to the NotificationManager:
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

		// Instantiate the Notification:
		int icon = R.drawable.icon;
		CharSequence tickerText = getString(R.string.app_name);
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);

		// Define the notification's message and PendingIntent:
		Context context = getApplicationContext();
		Intent notificationIntent = new Intent(this, MainActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);

		// Pass the Notification to the NotificationManager:
		mNotificationManager.notify(SPEEDMATE_NOTI_ID, notification);
	}
	final int SPEEDMATE_NOTI_ID = 1000;


	void showPreview1() {
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage(getString(R.string.popup_message));
		alt_bld.setPositiveButton(getString(R.string.button_ok), null);

		AlertDialog alert = alt_bld.create();
		alert.setTitle(getString(R.string.popup_title));
		alert.setIcon(R.drawable.icon);
		alert.show();
	}

	void showPreiview2() {
		Intent intent = new Intent(this, SampleActivity.class);
		startActivity(intent);
	}

	float[] values = new float[21];

	int indexFromValue(float value) {
		for (int i = 0; i < values.length; i++) {
			if (values[i] == value) {
				return i;
			}
		}
		return 0;
	}

	float valueFromIndex(int index) {
		if (index < 0 || index >= values.length) {
			return (float) 0.0;
		}
		return values[index];
	}

	ArrayList<String> datas = new ArrayList<String>();
	public String getStringValue(int index){
		if (datas.size() == 0) {
			for (int i = -10; i < 11; i++) {
				datas.add(""+i);
			}
		}
		return datas.get(index);
	}

	public static final int ID_MENU	= 100;
	public static final int ID_MENU_SET	= 101;
	public static final int ID_MENU_UNSET	= 102;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		log("onCreateOptionsMenu>>");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		log("onOptionsItemSelected>>");
		
		int itemID = item.getItemId();
		if (itemID == ID_MENU_SET) {
			setMenuSetting(0);		
			return true;
		}else if(itemID == ID_MENU_UNSET){
			setMenuSetting(1);
			return true;
		}
		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		log("onPrepareOptionsMenu>>");
		menu.clear();
		SubMenu menuConfig = menu.addSubMenu(getString(R.string.menu));
		if (getMenuSetting() == 0) {
			menuConfig.add(ID_MENU, ID_MENU_SET, 1, R.string.menu_set).setChecked(true);
			menuConfig.add(ID_MENU, ID_MENU_UNSET, 2, R.string.menu_unset);
		}else{
			menuConfig.add(ID_MENU, ID_MENU_SET, 1, R.string.menu_set);
			menuConfig.add(ID_MENU, ID_MENU_UNSET, 2, R.string.menu_unset).setChecked(true);
		}
		menuConfig.setGroupCheckable(ID_MENU, true, true);
		return super.onPrepareOptionsMenu(menu);
	}
	
	static final String PrefName = "SpeedMate_Pref";
	static final String KEY_SETTING = "Setting";
	int getMenuSetting(){
		SharedPreferences prefs = getSharedPreferences(PrefName, MODE_PRIVATE);
		int retValue = prefs.getInt(KEY_SETTING, 0);
		log("getMenuSetting:"+retValue);
		return retValue; 
	}
	
	void setMenuSetting(int setting){
		log("setMenuSetting:"+setting);
		
		SharedPreferences prefs = getSharedPreferences(PrefName, MODE_PRIVATE); 
		SharedPreferences.Editor editor = prefs.edit(); 
		editor.putInt(KEY_SETTING, setting); 
		editor.commit();
	}
	
	void log(String message){
//		Log.d("SPEEDMATE", message);
	}
	
	
	
	

}
